/*
 * Feb/13/2013
 * 
 * Group 2
 * ITCS 3146
 * 
 * Group Project
 * MULTICS Paging Algorithm
 * 
 */


package MULTICS;

public class Page 
{
	
	private String physicalAddress;
	
	// To Make Page Replacement Simpler
	private PageDescriptor pageHeld; 
	
	private SegmentDescriptor pageSegment;
	
	// For Clock Page Replacement Algorithm
	private boolean rBit; 
	
	

	// Sets physical address, assign pageDescriptor to page, set rbit, assign segmentDescriptor to page
	Page(int pageFrameNumb,PageDescriptor descriptor, SegmentDescriptor pageSegment) 
	{
		// Create physical address
		physicalAddress = Integer.toBinaryString(pageFrameNumb * 1024);
		
		// Adds padding to address
		
		if(physicalAddress.length() <24)
		{
			
			String padding = "";
			for(int i = 0;i < 24-physicalAddress.length();i++)
			{
				padding = padding + "0";
			}
			
			physicalAddress = padding + physicalAddress;
			
		}
		
		pageHeld =  descriptor;
		rBit = descriptor.returnReferenced();
		this.pageSegment = pageSegment;

	}
	
	// Returns the segment belonging to the page in RAM
	public SegmentDescriptor returnHeldSeg()
	{
		return pageSegment;
	}
	
	
	// Returns pageDescriptor of page in memory
	public PageDescriptor returnHeldPage()
	{
		return pageHeld;
	}
	
	// Returns rbit
	public boolean returnRBit()
	{
		return rBit;
	}
	
	// set rbit
	public void setRBit(boolean status)
	{
		pageHeld.setPresAbs(status);
		rBit = status;
	}

	// Returns physical address
	public String returnPhysicalAddress()
	{
		return physicalAddress;
	}
	
	// Returns a 24bit physical address
	public String applyOffset(int offset) 
	{
		
		String offsetAddress = null;
		offsetAddress = Integer.toBinaryString(offset + Integer.parseInt(physicalAddress,2));
				
		if(offsetAddress.length() <24)
		{
			
			String padding = "";
			for(int i = 0;i < 24-offsetAddress.length();i++)
			{
				padding = padding + "0";
			}
			
			offsetAddress = padding + offsetAddress;
			
		}
		
		
		return offsetAddress;
	}

}
